package org.aurora.core.server.scheduler.processor;

import org.apache.commons.collections4.CollectionUtils;
import org.aurora.model.request.ClientConnectRequest;
import org.aurora.model.response.CommonResponse;
import org.aurora.remote.AbstractRemoteProcessor;

import java.util.*;

@Deprecated
public class ClientConnectProcessor extends AbstractRemoteProcessor<ClientConnectRequest, CommonResponse> {

    private final Map<String/*appcode*/, /*ip+:+port*/SortedSet<String>> appcodeToClientsMap = new HashMap<>();

    @Override
    public CommonResponse doProcessor(ClientConnectRequest request) {
        String appcode = request.getAppcode();
        return null;
    }

    public synchronized void putClient(String appcode, String addr) {
        this.appcodeToClientsMap.computeIfAbsent(appcode, key -> new TreeSet<String>()).add(addr);
    }

    public synchronized void removeClient(String addr) {
        this.appcodeToClientsMap.remove(addr);
    }

    public synchronized List<String> getAppcodeClientAddrList(String appcode) {
        SortedSet<String> clientAddrSet = this.appcodeToClientsMap.get(appcode);

        if (CollectionUtils.isEmpty(clientAddrSet)) {
            return null;
        }

        return new ArrayList<>(clientAddrSet);
    }
}
